function res = resid_dim5case5_pscc(x,shat_3,S_3,la,u,mu0,phi,kap0,eta0,be,del,d,Pzz,cz,T_guess,M)
%function solving for equilibrium (given lambda) in case where all firms actively sell in top two productivity 
%states, all firms randomize in next productivity state, and no firm actively sells in other states

options = optimoptions('fsolve','TolFun',1e-9,'TolX',1e-9,'StepTolerance',1e-9,'Display','none');

shat_1=x(1);shat_2=x(2);al_3=x(3);dim=5;

%solve for theta's corresponding to shat's 
th_res = @(th) la-mu_pscc(th,mu0,eta0)*eta_el_pscc(th,mu0,eta0)/(1-eta_el_pscc(th,mu0,eta0))*shat_1*kap_s_pscc(shat_1,kap0,phi)/(1+eta_pscc(th,mu0,eta0)*shat_1);
[th_1,fval,exit_flag1] = fsolve(th_res,T_guess(1),options);
th_res = @(th) la-mu_pscc(th,mu0,eta0)*eta_el_pscc(th,mu0,eta0)/(1-eta_el_pscc(th,mu0,eta0))*shat_2*kap_s_pscc(shat_2,kap0,phi)/(1+eta_pscc(th,mu0,eta0)*shat_2);
[th_2,fval,exit_flag2] = fsolve(th_res,T_guess(2),options);
if min([exit_flag1,exit_flag2])<1,disp('issue: case 5');end

%match surpluses given active selling behavior
S_1=kap_s_pscc(shat_1,kap0,phi)/eta_pscc(th_1,mu0,eta0)+la/mu_pscc(th_1,mu0,eta0);
S_2=kap_s_pscc(shat_2,kap0,phi)/eta_pscc(th_2,mu0,eta0)+la/mu_pscc(th_2,mu0,eta0);

%match surpluses given behavior in active selling states, randomizatiom state, and otherwise
S_vec=M*(u*ones(dim,1)-cz+be*(1-del)*(1-d)*(-la*ones(dim,1)-Pzz(:,1)*kap_n_pscc(shat_1,kap0,phi)-Pzz(:,2)*kap_n_pscc(shat_2,kap0,phi)-Pzz(:,3)*al_3*kap_n_pscc(shat_3,kap0,phi)));

%equilibrium consistency conditions for surplus
res=[S_vec(1)-S_1;S_vec(2)-S_2;S_vec(3)-S_3];
end